package com.ldzy.controller;

import com.ldzy.pojo.Cake;
import com.ldzy.pojo.Order;
import com.ldzy.pojo.Pay;
import com.ldzy.pojo.User;
import com.ldzy.service.ICakeService;
import com.ldzy.service.IOrderService;
import com.ldzy.service.IPayService;
import com.ldzy.service.IUserService;
import com.ldzy.utilts.RespUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pay")
public class PayController {
    @Resource
    IPayService iPayService;
    @Resource
    IOrderService iOrderService;
    @Resource
    ICakeService iCakeService;
    @Resource
    IUserService iUserService;
    @PostMapping("/updateAddress")
    public RespUtil updateAddress(@RequestBody Pay pay){
        System.out.println(pay);
        return  iPayService.updateAddress(pay);
    }
    @PostMapping("/findByPayUser")
    public  RespUtil findByPayUser(@RequestBody Pay pay){
        return  iPayService.findByPayUser(pay);
    }
    @PostMapping("/findPayById")
    public  RespUtil findPayById(@RequestBody Pay pay){
//        System.out.println(pay);
        if (iPayService.getById(pay.getId())!=null)
            return  RespUtil.success(iPayService.getById(pay.getId()));
        return  RespUtil.error();
    }
    @PostMapping("/AllOrder")
    public  RespUtil AllOrder (@RequestBody Pay pay){
        //创建map集合
        List<Map<String, Object>> list= new ArrayList<>();
        //判断是否有这个支付信息
        if (iPayService.getById(pay.getId())!=null)
            pay =iPayService.getById(pay.getId());
        //根据支付信息获取订单，并且保存在数组里面
        Order order =new Order();
        order.setOrderId(pay.getOrderId());
        List<Order> orderList = iOrderService.orderList(order);
        for (int i = 0; i <orderList.size() ; i++) {
            Cake cake =new Cake();
            Map<String, Object> objectMap =  new HashMap<>();
//            Map<String, Double>  doubleMap =new HashMap<>();
            Order order1 = new Order();
            //根据订单id查询订单祥情，并保存在Map里
            order1.setId(orderList.get(i).getId());
            order1=iOrderService.order(order1);
            objectMap.put("order",order1);
            //计算每件商品价格,并且保存在map
            double money= order1.getMoney()*order1.getNumber();
            objectMap.put("money",money);
            //获取商品id
            cake.setId(orderList.get(i).getCakeId());
            //根据商品id来获取商品详情,并且保存在map
            objectMap.put("cake",iCakeService.getById(cake));
            //把map添加至map集合
            list.add(objectMap);

        }
        //返回map集合至前端
        return  RespUtil.success(list);
    }
    @PostMapping("/")
    public  RespUtil pay(@RequestBody Pay pay){
        System.out.println(pay);
        Integer number=0;
        pay=iPayService.getById(pay.getId());
        System.out.println(pay);
        if (pay==null){
          return   RespUtil.error("系统错误!!!");
        }

        //根据用户id查询用户
        User user=iUserService.getById(pay.getUserId());
        if(user.getUserMoney()<pay.getMoney()){
            return  RespUtil.error("账户余额不足,支付失败");
        }
        //保存支付金额
        user.setUserMoney(pay.getMoney());
        if (iUserService.moneyPay(user)){
            //根据支付信息获取订单，并且保存在数组里面
            Order order =new Order();
            order.setOrderId(pay.getOrderId());
            List<Order> orderList = iOrderService.orderList(order);
            for (int i = 0; i <orderList.size() ; i++) {
                Cake cake =new Cake();
                Order order1 = new Order();
                //根据订单id查询订单祥情
                order1.setId(orderList.get(i).getId());
                order1=iOrderService.order(order1);
                //根据商品id查询商品
                cake=iCakeService.getById(order1.getCakeId());
                //修改商品推销量
                 number = cake.getCakeNumber()+order1.getNumber();

                System.out.println(number);
                cake.setCakeNumber(number);
                iCakeService.cakeNumber(cake);
            }
            //修改支付状态
            iPayService.updatePayType(pay);
            return  RespUtil.success("支付成功!!!",iUserService.getById(user.getUserId()));
        }
      return  RespUtil.error("系统错误，支付失败！！！");
    }
    @PostMapping("/delete")
    public  RespUtil delete (@RequestBody Pay pay){
        if(iPayService.removeById(pay.getId()))
            return RespUtil.success("删除成功!!!");
        return  RespUtil.error("删除失败！！！");
    }
}
